
const initArticles = async (redis) => {
  const {articlesModule} =require('../ORM/public/Model')
  // 检查 articlesList 的值而不是仅仅检查存在性
  const articlesListCacheDate = await redis.ttl('articlesList'); 
  const articlesListisExist = await redis.exists('articlesList');
  if (articlesListCacheDate==-1||!articlesListisExist) {
  const articles = await articlesModule.findAll({
    order: [['createdAt', 'DESC']],
  })
  for (const article of articles) {
    await redis.hSet(`like:${article.id}`, "count", 0);
    await redis.hSet(`comment:${article.id}`, "count", 0);
    const likeCount = await redis.hGet(`like:${article.id}`, "count");
    const commentCount = await redis.hGet(`comment:${article.id}`, "count");
    const articleData = {
      id: article.id,
      title: article.title,
      cover: article.cover,
      content: article.content,
      like: parseInt(likeCount, 10),
      comment: parseInt(commentCount, 10),
      category_id: article.category_id,
      createdAt: article.createdAt,
      status:article.status
    };
    await redis.rPush('articlesList', JSON.stringify(articleData));
    // 设置 articlesList 的过期时间 
    await redis.expire('articlesList', 48 * 60 * 60);
  }
  }
  else{
    console.log('articlesList未过期');
  }

};
module.exports = initArticles;